package cn.edu.laud.sany.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class MyStatisticLog
{
	private FileWriter fWriter;
	
	public static MyStatisticLog getMyStatisticLog(String fileName)
	{
		MyStatisticLog log = new MyStatisticLog();
		File file = new File(fileName);
		if (file.exists() == false)
		{
			try
			{
				file.createNewFile();
			}
			catch (IOException e)
			{
				e.printStackTrace();
				return null;
			}
		}
		try
		{
			log.fWriter = new FileWriter(file, true);
		}
		catch (IOException e)
		{
			e.printStackTrace();
			return null;
		}
		return log;
	}
	
	public String getLogWithTime(String log)
	{
		StringBuilder sb = new StringBuilder();
		sb.append(String.valueOf(System.nanoTime()));
		sb.append(" ");
		sb.append(log);
		return sb.toString();
	}
	
	public synchronized void printLog(String log)
	{
		String printLogString = getLogWithTime(log);
		try
		{
			fWriter.write(printLogString);
			fWriter.write("\r\n");
			fWriter.flush();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public void log(String log)
	{
		printLog(log);
	}
	
}
